package com.czkj.hwz.decorate.fragment;


import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.czkj.hwz.decorate.IMP.HttpDoImp;
import com.czkj.hwz.decorate.base.BaseFragment;
import com.czkj.hwz.decorate.base.HttpUrl;
import com.czkj.hwz.decorate.bean.Enterprise;
import com.czkj.hwz.decorate.bean.Goods;
import com.czkj.hwz.decorate.control.GoodsGet;
import com.czkj.hwz.decorate.view.banner.CustomViewPager;
import com.czkj.hwz.decorate.view.banner.MyGridView;
import com.czkj.hwz.decorate.R;


import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link GoodsFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
@ContentView(R.layout.fragment_goods)
public class GoodsFragment extends BaseFragment implements HttpDoImp {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    private static final String ARG_PARAM3 = "param3";
    // TODO: Rename and change types of parameters
    private Enterprise mParam1;
    public static CustomViewPager vp;
    private int index;


    @ViewInject(R.id.gv)
    MyGridView gv;
    private ArrayList<Goods> datas = new ArrayList<>();
    MyAdapter mMyAdapter;
    GoodsGet mGoodsGet;

    public GoodsFragment() {
        // Required empty public constructor
    }

    public static GoodsFragment newInstance(Enterprise param1, int index) {
        GoodsFragment fragment = new GoodsFragment();
        Bundle args = new Bundle();
        args.putSerializable(ARG_PARAM1, param1);
        args.putInt(ARG_PARAM3, index);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = (Enterprise) getArguments().getSerializable(ARG_PARAM1);

            index = getArguments().getInt(ARG_PARAM3);
        }
        mGoodsGet = new GoodsGet(getContext(), this);

    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        vp.setObjectForPosition(view, index);

    }


    public void getData(Enterprise enterprise) {
        HashMap<String, String> reqParams = new HashMap<>();
        reqParams.put("enterpriseid", String.valueOf(enterprise.getId()));
        mGoodsGet.getList(reqParams);


    }

    @Override
    public void onSuccess(String flag, Object... objects) {
        int row = (int) objects[0];
        if (row <= 0) {
//            datas = new ArrayList<>();
//            Goods goods = new Goods();
//            goods.setName("");
//            datas.add(goods);
            datas.clear();
        }else {
            datas = (ArrayList<Goods>) objects[1];

//            Goods goods = new Goods();
//            goods.setName("");
//            datas.add(goods);
//            Goods goods2 = new Goods();
//            goods2.setName("");
//            datas.add(goods2);
        }

        mMyAdapter = new MyAdapter(getContext(), datas);
        gv.setAdapter(mMyAdapter);

    }

    @Override
    public void onError(String errinfo) {
        Toast.makeText(getContext(), errinfo, Toast.LENGTH_LONG).show();
    }

    class MyAdapter extends BaseAdapter {
        private Context mContext;
        private ArrayList<Goods> datas;
        private String imgUrlStr;
        ViewHolder holder;
        String titleStr = "";

        public MyAdapter(Context mContext) {
            this.mContext = mContext;
        }

        public MyAdapter(Context context, ArrayList<Goods> datas) {
            mContext = context;
            this.datas = datas;
        }

        @Override
        public int getCount() {
            return datas.size();
        }

        @Override
        public Goods getItem(int i) {
            return datas.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {

            if (view == null) {
                view = LayoutInflater.from(mContext).inflate(R.layout.grideview_goods_item_shouye, viewGroup, false);
                holder = new ViewHolder();
                holder.img = (ImageView) view.findViewById(R.id.img);

                holder.tv_title = (TextView) view.findViewById(R.id.tv_title);
                view.setTag(holder);
            } else {
                holder = (ViewHolder) view.getTag();
            }

            final Goods item = datas.get(i);
            if (null != item) {
                titleStr = "";
                if (item.isSelfSupport()) {
                    titleStr = "<font color='#FFB400'>【自营】</font>  ";
                }
                titleStr = titleStr + item.getName();
                holder.tv_title.setText(Html.fromHtml(titleStr));
                if (!TextUtils.isEmpty(item.getSmallImg())) {
                    imgUrlStr = HttpUrl.BASE_URL + item.getSmallImg();
                    x.image().bind(holder.img, imgUrlStr);
                }
                view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(getContext(),item.getName(),Toast.LENGTH_SHORT).show();
                    }
                });
            }

            return view;
        }
    }

    static class ViewHolder {
        ImageView img;
        TextView tv_title;
    }

    @Override
    protected void lazyLoad() {
        getData(mParam1);
    }
}
